package net.zjitc.summary.student;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;

/**
 * @Author admin
 * @Date 2019/5/5 7:52
 * 测试类（界面）
 */

public class Test {
    static StudentDao dao=new StudentDaoDB();
    static Scanner scanner = new Scanner(System.in);
    static Scanner scannerInt = new Scanner(System.in);

    public static void main(String[] args) {
        while (true) {
            System.out.println("欢迎使用学籍管理系统：");
            System.out.println("1--查询学生名单");
            System.out.println("2--添加新学生信息");
            System.out.println("3--更新学生信息");
            System.out.println("4--删除学生信息");
            System.out.println("5--退出系统");
            System.out.println("请输入你的选择（1-5）：");
            int choose = scannerInt.nextInt();
            switch (choose) {
                case 1:
                    selectUI();
                    break;
                case 2:
                    insertUI();
                    break;
                case 3:
                    updateUI();
                    break;
                case 4:
                    deleteUI();
                    break;
                case 5:
                    System.out.println("欢迎下次光临");
                    /* 退出之前，先关闭数据库相关连接*/
                    dao.close();
                    System.exit(0);
                    break;
                default:
                    System.out.println("输入有误，请输入1-5：");
            }
        }
    }

    private static void insertUI() {
        System.out.println();
        System.out.println("-----添加界面------");
        System.out.print("请输入学生学号：");
        String id = scanner.nextLine();
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入性别：");
        String sex = scanner.nextLine();
        System.out.print("请输入年龄：");
        int age=scannerInt.nextInt();
        System.out.print("请输入班级：");
        String className=scanner.nextLine();
        Student student=new Student(id,name,sex,age,className);
        /* 判断是否已经存在该学号，若存在提示，若不存在 保存该对象*/
        Student s=dao.selectById(id);
        if(s==null){
            int c=dao.insert(student);
            if(c>0){
                System.out.println("添加成功");
            }else{
                System.out.println("添加失败！");
            }
        }else{
            System.out.println("失败！已存在该学号，重复了！");
        }
    }

    private static void selectUI() {
        System.out.println("-----所有学生信息如下：-------");
        System.out.println("\t学号\t姓名\t性别\t年龄\t班级");
        /* 查询返回，返回保存学生对象的集合*/
        ArrayList<Student> arrayList=dao.selectAll();
        /* 遍历集合，显示*/
        Iterator<Student> iterator=arrayList.iterator();
        while (iterator.hasNext()){
            Student student=iterator.next();
            System.out.println(student.getId()+"\t"+student.getName()+"\t"
            +student.getSex()+"\t"+student.getAge()+"\t"+student.getClassName());
        }
    }

    private static void updateUI() {
        System.out.println("-----更新界面-----");
        System.out.println("请输入要修改的学生学号：");
        String id = scanner.nextLine();
        /* 先判断是否存在，存在才做更新操作*/
        Student s=dao.selectById(id);
        if (s == null) {
            System.out.println("你要更新的学生不存在");
        } else {
            System.out.println("\t学号\t姓名\t性别\t年龄\t班级");
            System.out.println(s.getId()+"\t"+s.getName()
                    +"\t"+s.getSex()+"\t"+s.getAge()+"\t"
                    +s.getClassName());
            System.out.println();
            System.out.print("请输入姓名：");
            String name = scanner.nextLine();
            System.out.print("请输入性别：");
            String sex = scanner.nextLine();
            System.out.print("请输入年龄：");
            int age=scannerInt.nextInt();
            System.out.print("请输入班级：");
            String className=scanner.nextLine();

            Student student=new Student(id,name,sex,age,className);
            int c=dao.update(student);
            if(c>0){
                System.out.println("更新成功！");
            }else{
                System.out.println("更新失败！");
            }

        }
    }

    private static void deleteUI() {
        System.out.println("-----删除界面-----");
        System.out.println("请输入要删除的学生学号：");
        String id = scanner.nextLine();
        /* 先判断，存在再删除*/
        Student student=dao.selectById(id);
        if(student==null){
            System.out.println("要删除的学生不存在");
        }else{
            int c=dao.delete(id);
            if(c>0){
                System.out.println("删除成功");
            }else{
                System.out.println("删除失败");
            }
        }

    }
}

